package com.whut.customer.controller;


import com.whut.common.util.R;
import com.whut.customer.base.BaseController;
import com.whut.customer.entity.SysMenu;
import com.whut.customer.service.ISysMenuService;
import com.whut.customer.util.ValidatorUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author author
 * @since 2021-01-23
 */
@RestController
@RequestMapping("/sysMenu")
public class SysMenuController extends BaseController {

    @Autowired
    private ISysMenuService menuService;

    /**
     * 添加菜单,设置一级菜单的parentId=0
     */
    @ApiOperation("添加菜单")
    @PostMapping("/addMenu")
//    @RequiresPermissions("menu:add_menu")
    public R<SysMenu> addMenu(@RequestBody SysMenu menu) {
        Long userId= super.userInfo().getId();
        menu.setCreateDate(LocalDateTime.now());
        menu.setUpdateDate(LocalDateTime.now());
        menu.setCreateUserId(userId);
        menu.setUpdateUserId(userId);

        return R.ok(menuService.addMenu(menu));
    }

    /**
     * 编辑菜单信息
     */
    @PostMapping("/updateMenu")
//    @RequiresPermissions("menu:update_menu")
    public R<SysMenu> updateMenu(@RequestBody SysMenu menu) {
        Long userId= super.userInfo().getId();
        menu.setUpdateDate(LocalDateTime.now());
        menu.setUpdateUserId(userId);

        return R.ok(menuService.updateMenu(menu));
    }

    /**
     * 删除菜单 设置成不能选中上级直接删除，可以对下级进行多选，批量删除
     * 只需要传要删除对象的ID即可
     */
    @PostMapping("/deleteMenu")
//    @RequiresPermissions("menu:delete_menu")
    public R deleteMenu(@RequestBody SysMenu menu) {
        Long userId=super.userInfo().getId();
        boolean b = menuService.deleteMenu(menu,userId);
        return R.ok(b);
    }

    /**
     * 按条件查询菜单信息
     * 如果传入的参数为空对象，则返回全部未删除的菜单信息（树）
     * 如果有参数，则按条件查询（不是树）
     *
     */
    @PostMapping("/selectMenuByCondiction")
//    @RequiresPermissions("menu:select_menu")
    public R<List<SysMenu>> selectMenuByCondiction(@RequestBody SysMenu menu){
        List<SysMenu> list = new ArrayList<>(0);
        boolean isNull = ValidatorUtil.allEntityfieldIsNUll(menu);
        if(isNull) {
            menu.setDelFlag(0);
            menu.setMenuParentId(0L);
            list = menuService.selectMenuTree(menu);
        }
        else
            list = menuService.selectMenuByCondiction(menu);
        return R.ok(list);
    }

}
